/**
 * Created by Administrator on 2018/11/28.
 */
function borrowingList($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q){
    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;

    $CertusJsonAjax.dataInit("borrowingList",null,borrowingColumns,'borrowingContainer','borrowingToolBarContainer');
}

function addBorrowing($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q, backlogPersonList){
    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $backlogPersonList = backlogPersonList;

    $$scope.borrow = {}
    $$scope.borrow.personVisible = true;
    //personal select
    $$scope.personalSelect = [
        {name:"个人",code:"personal"},
        {name:"项目",code:"project"}
    ]

    $$scope.personChange = function(){
        if($$scope.borrow.borrowingType == "personal"){
            $$scope.borrow.borrowingName = "个人";
            $$scope.borrow.personVisible = true;
        }else{
            $$scope.borrow.borrowingName = "项目";
            $$scope.borrow.personVisible = false;
        }
    }

    $scope.$watch("borrow.projName", function (newValue, old, scope) {
        if (old && newValue) {
            if (newValue.indexOf(old) > -1) {
                nowProjName = newValue;
                scope.userProjSelect2 = scope.userProjSelect2.filter(isSaveProjName);
                if (scope.userProjSelect2.length) {
                    if (scope.userProjSelect2.length == 1) {
                        scope.borrow.projId = scope.userProjSelect2[0].projId;
                        scope.borrow.projName = scope.userProjSelect2[0].projName;
                    }
                }
                if(!scope.userProjSelect2 || scope.userProjSelect2.length == 0){
                    scope.borrow.projId = "";
                    scope.borrow.projName = "";
                }
                return;
            }
        }

        //直接去数据库 查询
        CertusFormAjax.ajax("relatedProj", {param: newValue}, function (res) {
            scope.userProjSelect2 = res.data;
            if (scope.userProjSelect2) {
                if (scope.userProjSelect2.length == 1) {
                    scope.borrow.projId = scope.userProjSelect2[0].projId;
                    scope.borrow.projName = scope.userProjSelect2[0].projName;
                }
            }else if(!scope.userProjSelect2 || scope.userProjSelect2.length == 0){
                scope.borrow.projId = "";
                scope.borrow.projName = "";
            }
        })
    })
    //提交 borrow
    $scope.borrowSubmit = function(){
        if(certusValidateNotnull()){
            if(!$$scope.borrow.personVisible){
                if($$scope.borrow.projId){
                    $CertusJsonAjax.ajax("borrowStart",$$scope.borrow,function(res){
                        if(res.data == "success"){
                            findBacklogPersonHead($CertusFormAjax, "JKSQLC");
                            history.back();
                        }
                    })
                }else{
                    layer.msg("请填写正确项目");
                }
            }else{
                $CertusJsonAjax.ajax("borrowStart",$$scope.borrow,function(res){
                    if(res.data == "success"){
                        findBacklogPersonHead($CertusFormAjax, "JKSQLC");
                        history.back();
                    }
                })
            }
        }
    }
}

function selBorrowing($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q){
    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;



    var borrowerCode = $routeParams.bowrrowingCode;

    $CertusFormAjax.ajax("borrowByCode",{
        "borrowCode":borrowerCode
    },function(res){

        $$scope.borrow = {}
        $$scope.borrow.personVisible = true;
        //personal select
        $$scope.personalSelect = [
            {name:"个人",code:"personal"},
            {name:"项目",code:"project"}
        ]


        $$scope.borrow = res.data;

        if($$scope.borrowingType == "personal"){
            $$scope.borrow.personVisible = true;
        }else{
            $$scope.borrow.personVisible = false;
        }

        $$scope.processs = res.data.processs;

    })

    CertusJsonAjax.ajaxNode("chargeGrid","regularData",{borrowCode:borrowerCode},function(data){


        //初始化 表格 chargeAgaist  chargeAgaistTool
        // 采购记录
        var chargeColumn = [
            {
                id: 'chargeType',
                title: '冲销类型',
                width: 73,
                type: 'string',
                columnClass: 'text-center',
                resolution: function (value, record, column, grid, dataNo, columnNo) {
                   if(value == "reception"){
                       return "招待报销冲销"
                   }else{
                       return "现金报销";
                   }
                }
            }, {
                id: 'chargeCode',
                title: '流程编码',
                width: 73,
                type: 'string',
                columnClass: 'text-center',
            }, {
                id: 'realChargeOff',
                title: '冲销金额',
                width: 73,
                type: 'string',
                columnClass: 'text-center',
                resolution: function (value, record, column, grid, dataNo, columnNo) {
                    var content = '￥' + value + '元';
                    return content;
                  }
            }, {
                id: 'userName',
                title: '操作人',
                width: 73,
                type: 'string',
                columnClass: 'text-center',
            }, {
                id: 'time',
                title: '时间',
                width: 200,
                type: 'string',
                columnClass: 'text-left',
                resolution: function (value, record, column, grid, dataNo, columnNo) {
                    if(record.time){
                        // var content = $.fn.dlshouwen.grid.tools.dateFormat(record.time, "yyyy-MM-dd hh:mm");
                        return record.time;
                    }
                    return "";
                }
            }];

        var tenderOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '投标文件列表',
            datas: data,
            columns: chargeColumn,
            gridContainer: 'chargeAgaist',
            toolbarContainer: 'chargeAgaistTool',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        var tenderGrid = $.fn.dlshouwen.grid.init(tenderOption);
        tenderGrid.load();
    })


    $$scope.getsssbbb = function(bb){
        if(bb == "starting"){
            return "正在进行中";
        }else if(bb == "denied"){
            return "否决";
        }else if(bb == "sentback"){
            return "被退";
        }else if(bb == "others"){
            return "关闭";
        }else if(bb == "completed"){
            return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
        return "待办";
    }

}

